package main

import (
	"context"
	"golang.org/x/sync/errgroup"
	"net/http"
	"os"
	"os/signal"
	"syscall"
)

func main() {
	signals := make(chan os.Signal)
	signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM)
	mux := http.NewServeMux()
	mux.HandleFunc("/healthz", healthz)
	srv := http.Server{
		Addr:    ":8080",
		Handler: mux,
	}
	g, ctx := errgroup.WithContext(context.Background())
	g.Go(func() error {
		return srv.ListenAndServe()
	})
	g.Go(func() error {
		for {
			select {
			case <-ctx.Done():
				return ctx.Err()
			case <-signals:
				return srv.Close()
			}
		}
	})
	g.Wait()
}

func healthz(w http.ResponseWriter, r *http.Request) {
	w.WriteHeader(200)
	w.Write([]byte("OK"))
}
